package cn.edu.scau.cmi.hombio.web.rest;

import cn.edu.scau.cmi.hombio.dao.EmployeeDAO;
import cn.edu.scau.cmi.hombio.dao.InspectionDAO;
import cn.edu.scau.cmi.hombio.dao.InspectionExceptionDAO;
import cn.edu.scau.cmi.hombio.dao.PigHouseDAO;
import cn.edu.scau.cmi.hombio.dao.TaskDAO;

import cn.edu.scau.cmi.hombio.domain.Employee;
import cn.edu.scau.cmi.hombio.domain.Inspection;
import cn.edu.scau.cmi.hombio.domain.InspectionException;
import cn.edu.scau.cmi.hombio.domain.PigHouse;
import cn.edu.scau.cmi.hombio.domain.Task;

import cn.edu.scau.cmi.hombio.service.InspectionService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Spring Rest controller that handles CRUD requests for Inspection entities
 * 
 */

@Controller("InspectionRestController")

public class InspectionRestController {

	/**
	 * DAO injected by Spring that manages Employee entities
	 * 
	 */
	@Autowired
	private EmployeeDAO employeeDAO;

	/**
	 * DAO injected by Spring that manages Inspection entities
	 * 
	 */
	@Autowired
	private InspectionDAO inspectionDAO;

	/**
	 * DAO injected by Spring that manages InspectionException entities
	 * 
	 */
	@Autowired
	private InspectionExceptionDAO inspectionExceptionDAO;

	/**
	 * DAO injected by Spring that manages PigHouse entities
	 * 
	 */
	@Autowired
	private PigHouseDAO pigHouseDAO;

	/**
	 * DAO injected by Spring that manages Task entities
	 * 
	 */
	@Autowired
	private TaskDAO taskDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for Inspection entities
	 * 
	 */
	@Autowired
	private InspectionService inspectionService;

	/**
	 * Create a new PigHouse entity
	 * 
	 */
	@RequestMapping(value = "/Inspection/{inspection_id}/pigHouse", method = RequestMethod.POST)
	@ResponseBody
	public PigHouse newInspectionPigHouse(@PathVariable Integer inspection_id, @RequestBody PigHouse pighouse) {
		inspectionService.saveInspectionPigHouse(inspection_id, pighouse);
		return pigHouseDAO.findPigHouseByPrimaryKey(pighouse.getId());
	}

	/**
	* View an existing Task entity
	* 
	*/
	@RequestMapping(value = "/Inspection/{inspection_id}/task/{task_id}", method = RequestMethod.GET)
	@ResponseBody
	public Task loadInspectionTask(@PathVariable Integer inspection_id, @PathVariable Integer related_task_id) {
		Task task = taskDAO.findTaskByPrimaryKey(related_task_id, -1, -1);

		return task;
	}

	/**
	* Get Employee entity by Inspection
	* 
	*/
	@RequestMapping(value = "/Inspection/{inspection_id}/employee", method = RequestMethod.GET)
	@ResponseBody
	public Employee getInspectionEmployee(@PathVariable Integer inspection_id) {
		return inspectionDAO.findInspectionByPrimaryKey(inspection_id).getEmployee();
	}

	/**
	* View an existing PigHouse entity
	* 
	*/
	@RequestMapping(value = "/Inspection/{inspection_id}/pigHouse/{pighouse_id}", method = RequestMethod.GET)
	@ResponseBody
	public PigHouse loadInspectionPigHouse(@PathVariable Integer inspection_id, @PathVariable Integer related_pighouse_id) {
		PigHouse pighouse = pigHouseDAO.findPigHouseByPrimaryKey(related_pighouse_id, -1, -1);

		return pighouse;
	}

	/**
	* Show all Inspection entities
	* 
	*/
	@RequestMapping(value = "/Inspection", method = RequestMethod.GET)
	@ResponseBody
	public List<Inspection> listInspections() {
		return new java.util.ArrayList<Inspection>(inspectionService.loadInspections());
	}

	/**
	* Create a new Employee entity
	* 
	*/
	@RequestMapping(value = "/Inspection/{inspection_id}/employee", method = RequestMethod.POST)
	@ResponseBody
	public Employee newInspectionEmployee(@PathVariable Integer inspection_id, @RequestBody Employee employee) {
		inspectionService.saveInspectionEmployee(inspection_id, employee);
		return employeeDAO.findEmployeeByPrimaryKey(employee.getId());
	}

	/**
	* View an existing Employee entity
	* 
	*/
	@RequestMapping(value = "/Inspection/{inspection_id}/employee/{employee_id}", method = RequestMethod.GET)
	@ResponseBody
	public Employee loadInspectionEmployee(@PathVariable Integer inspection_id, @PathVariable Integer related_employee_id) {
		Employee employee = employeeDAO.findEmployeeByPrimaryKey(related_employee_id, -1, -1);

		return employee;
	}

	/**
	* Create a new Task entity
	* 
	*/
	@RequestMapping(value = "/Inspection/{inspection_id}/task", method = RequestMethod.POST)
	@ResponseBody
	public Task newInspectionTask(@PathVariable Integer inspection_id, @RequestBody Task task) {
		inspectionService.saveInspectionTask(inspection_id, task);
		return taskDAO.findTaskByPrimaryKey(task.getId());
	}

	/**
	* Save an existing PigHouse entity
	* 
	*/
	@RequestMapping(value = "/Inspection/{inspection_id}/pigHouse", method = RequestMethod.PUT)
	@ResponseBody
	public PigHouse saveInspectionPigHouse(@PathVariable Integer inspection_id, @RequestBody PigHouse pighouse) {
		inspectionService.saveInspectionPigHouse(inspection_id, pighouse);
		return pigHouseDAO.findPigHouseByPrimaryKey(pighouse.getId());
	}

	/**
	* Select an existing Inspection entity
	* 
	*/
	@RequestMapping(value = "/Inspection/{inspection_id}", method = RequestMethod.GET)
	@ResponseBody
	public Inspection loadInspection(@PathVariable Integer inspection_id) {
		return inspectionDAO.findInspectionByPrimaryKey(inspection_id);
	}

	/**
	* Delete an existing Task entity
	* 
	*/
	@RequestMapping(value = "/Inspection/{inspection_id}/task/{task_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteInspectionTask(@PathVariable Integer inspection_id, @PathVariable Integer related_task_id) {
		inspectionService.deleteInspectionTask(inspection_id, related_task_id);
	}

	/**
	* Get Task entity by Inspection
	* 
	*/
	@RequestMapping(value = "/Inspection/{inspection_id}/task", method = RequestMethod.GET)
	@ResponseBody
	public Task getInspectionTask(@PathVariable Integer inspection_id) {
		return inspectionDAO.findInspectionByPrimaryKey(inspection_id).getTask();
	}

	/**
	* Show all InspectionException entities by Inspection
	* 
	*/
	@RequestMapping(value = "/Inspection/{inspection_id}/inspectionExceptions", method = RequestMethod.GET)
	@ResponseBody
	public List<InspectionException> getInspectionInspectionExceptions(@PathVariable Integer inspection_id) {
		return new java.util.ArrayList<InspectionException>(inspectionDAO.findInspectionByPrimaryKey(inspection_id).getInspectionExceptions());
	}

	/**
	* Save an existing Employee entity
	* 
	*/
	@RequestMapping(value = "/Inspection/{inspection_id}/employee", method = RequestMethod.PUT)
	@ResponseBody
	public Employee saveInspectionEmployee(@PathVariable Integer inspection_id, @RequestBody Employee employee) {
		inspectionService.saveInspectionEmployee(inspection_id, employee);
		return employeeDAO.findEmployeeByPrimaryKey(employee.getId());
	}

	/**
	* View an existing InspectionException entity
	* 
	*/
	@RequestMapping(value = "/Inspection/{inspection_id}/inspectionExceptions/{inspectionexception_id}", method = RequestMethod.GET)
	@ResponseBody
	public InspectionException loadInspectionInspectionExceptions(@PathVariable Integer inspection_id, @PathVariable Integer related_inspectionexceptions_id) {
		InspectionException inspectionexception = inspectionExceptionDAO.findInspectionExceptionByPrimaryKey(related_inspectionexceptions_id, -1, -1);

		return inspectionexception;
	}

	/**
	* Save an existing Inspection entity
	* 
	*/
	@RequestMapping(value = "/Inspection", method = RequestMethod.PUT)
	@ResponseBody
	public Inspection saveInspection(@RequestBody Inspection inspection) {
		inspectionService.saveInspection(inspection);
		return inspectionDAO.findInspectionByPrimaryKey(inspection.getId());
	}

	/**
	* Delete an existing Employee entity
	* 
	*/
	@RequestMapping(value = "/Inspection/{inspection_id}/employee/{employee_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteInspectionEmployee(@PathVariable Integer inspection_id, @PathVariable Integer related_employee_id) {
		inspectionService.deleteInspectionEmployee(inspection_id, related_employee_id);
	}

	/**
	* Delete an existing InspectionException entity
	* 
	*/
	@RequestMapping(value = "/Inspection/{inspection_id}/inspectionExceptions/{inspectionexception_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteInspectionInspectionExceptions(@PathVariable Integer inspection_id, @PathVariable Integer related_inspectionexceptions_id) {
		inspectionService.deleteInspectionInspectionExceptions(inspection_id, related_inspectionexceptions_id);
	}

	/**
	* Save an existing Task entity
	* 
	*/
	@RequestMapping(value = "/Inspection/{inspection_id}/task", method = RequestMethod.PUT)
	@ResponseBody
	public Task saveInspectionTask(@PathVariable Integer inspection_id, @RequestBody Task task) {
		inspectionService.saveInspectionTask(inspection_id, task);
		return taskDAO.findTaskByPrimaryKey(task.getId());
	}

	/**
	* Delete an existing PigHouse entity
	* 
	*/
	@RequestMapping(value = "/Inspection/{inspection_id}/pigHouse/{pighouse_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteInspectionPigHouse(@PathVariable Integer inspection_id, @PathVariable Integer related_pighouse_id) {
		inspectionService.deleteInspectionPigHouse(inspection_id, related_pighouse_id);
	}

	/**
	* Register custom, context-specific property editors
	* 
	*/
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	* Get PigHouse entity by Inspection
	* 
	*/
	@RequestMapping(value = "/Inspection/{inspection_id}/pigHouse", method = RequestMethod.GET)
	@ResponseBody
	public PigHouse getInspectionPigHouse(@PathVariable Integer inspection_id) {
		return inspectionDAO.findInspectionByPrimaryKey(inspection_id).getPigHouse();
	}

	/**
	* Create a new Inspection entity
	* 
	*/
	@RequestMapping(value = "/Inspection", method = RequestMethod.POST)
	@ResponseBody
	public Inspection newInspection(@RequestBody Inspection inspection) {
		inspectionService.saveInspection(inspection);
		return inspectionDAO.findInspectionByPrimaryKey(inspection.getId());
	}

	/**
	* Save an existing InspectionException entity
	* 
	*/
	@RequestMapping(value = "/Inspection/{inspection_id}/inspectionExceptions", method = RequestMethod.PUT)
	@ResponseBody
	public InspectionException saveInspectionInspectionExceptions(@PathVariable Integer inspection_id, @RequestBody InspectionException inspectionexceptions) {
		inspectionService.saveInspectionInspectionExceptions(inspection_id, inspectionexceptions);
		return inspectionExceptionDAO.findInspectionExceptionByPrimaryKey(inspectionexceptions.getId());
	}

	/**
	* Create a new InspectionException entity
	* 
	*/
	@RequestMapping(value = "/Inspection/{inspection_id}/inspectionExceptions", method = RequestMethod.POST)
	@ResponseBody
	public InspectionException newInspectionInspectionExceptions(@PathVariable Integer inspection_id, @RequestBody InspectionException inspectionexception) {
		inspectionService.saveInspectionInspectionExceptions(inspection_id, inspectionexception);
		return inspectionExceptionDAO.findInspectionExceptionByPrimaryKey(inspectionexception.getId());
	}

	/**
	* Delete an existing Inspection entity
	* 
	*/
	@RequestMapping(value = "/Inspection/{inspection_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteInspection(@PathVariable Integer inspection_id) {
		Inspection inspection = inspectionDAO.findInspectionByPrimaryKey(inspection_id);
		inspectionService.deleteInspection(inspection);
	}
}